package com.cn.parks.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.parks.dao.FixedParkingDao;
import com.cn.parks.dao.UserCarDao;
import com.cn.parks.dao.impl.FixedParkingDaoImpl;
import com.cn.parks.dao.impl.UserCarDaoImpl;
import com.cn.parks.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * 固定车位停车记录管理Servlet
 */
@WebServlet("/fixedParking/*")
public class FixedParkingServlet extends HttpServlet {
    private FixedParkingDao fixedParkingDao = new FixedParkingDaoImpl();
    private UserCarDao userCarDao = new UserCarDaoImpl();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        String pathInfo = request.getPathInfo();
        
        try {
            if ("/list".equals(pathInfo)) {
                // 获取所有固定车位停车记录
                response.getWriter().write(JSON.toJSONString(Result.success(userCarDao.getAllCar())));
            } else if ("/stats".equals(pathInfo)){
                int fixedCount = userCarDao.fixedCountAll();
                System.out.println("fixedCount:" + fixedCount);
                int vipCount = userCarDao.vipCountAll();
                System.out.println("vipCount:" + vipCount);
                int count = fixedCount + vipCount;
                System.out.println("count:" + count);
                // 获取停车统计数据
                JSONObject result = new JSONObject();
                result.put("count", count);
                result.put("fixedCount",fixedCount);
                result.put("vipCount", vipCount);
                result.put("todayCount", userCarDao.todayCount());
                response.getWriter().write(JSON.toJSONString(Result.success(result)));
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (Exception e) {
            response.getWriter().write(JSON.toJSONString(Result.error(e.getMessage())));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        
        try {
            if ("/entry".equals(pathInfo)) {
                // 车辆入场
//                handleEntry(request, response);
            } else if ("/exit".equals(pathInfo)) {
                // 车辆出场
//                handleExit(request, response);
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (Exception e) {
            response.getWriter().write(JSON.toJSONString(Result.error(e.getMessage())));
        }
    }

//    private void listFixedParkings(HttpServletRequest request, HttpServletResponse response)
//            throws Exception {
//        List<FixedParking> parkings = fixedParkingDao.getAll();
//        request.setAttribute("parkings", parkings);
//        request.getRequestDispatcher("/WEB-INF/views/fixedParking/list.jsp").forward(request, response);
//    }
//
//    private void listByCard(HttpServletRequest request, HttpServletResponse response)
//            throws Exception {
//        String cardId = request.getParameter("cardId");
//        List<FixedParking> parkings = fixedParkingDao.getByCardId(cardId);
//        request.setAttribute("parkings", parkings);
//        request.getRequestDispatcher("/WEB-INF/views/fixedParking/byCard.jsp").forward(request, response);
//    }
//
//    private void handleEntry(HttpServletRequest request, HttpServletResponse response)
//            throws Exception {
//        FixedParking parking = new FixedParking();
//        parking.setFixedId(generateFixedId());
//        parking.setCardId(request.getParameter("cardId"));
//        parking.setEntryDate(new Date());
//        parking.setEntryTime(new Date());
//
//        fixedParkingDao.insert(parking);
//        response.sendRedirect(request.getContextPath() + "/fixedParking/list");
//    }
//
//    private void handleExit(HttpServletRequest request, HttpServletResponse response)
//            throws Exception {
//        String fixedId = request.getParameter("fixedId");
//        FixedParking parking = fixedParkingDao.getById(fixedId);
//
//        if (parking != null) {
//            parking.setOutDate(new Date());
//            parking.setOutTime(new Date());
//            fixedParkingDao.update(parking);
//        }
//
//        response.sendRedirect(request.getContextPath() + "/fixedParking/list");
//    }
//
//    private String generateFixedId() {
//        return "FIXED" + System.currentTimeMillis();
//    }
} 